package com.roncoo.eshop.inventory.request;

import com.roncoo.eshop.inventory.model.ProductInventory;
import com.roncoo.eshop.inventory.service.ProductInventoryService;

import lombok.extern.slf4j.Slf4j;

/**
 * Copyright: Copyright (c) 2019 Hu-Cheng
 * 
 * @ClassName: ProductInventoryCacheRefreshRequest.java
 * @Description: 重新加载商品库存的缓存
 *
 * @version: v1.0.0
 * @author: hucheng
 * @date: 2019年9月19日 下午3:31:52
 *
 */
@Slf4j
public class ProductInventoryCacheRefreshRequest implements Request {

	/**
	 * 商品ID
	 */
	private Integer productId;

	/**
	 * 商品库存Service
	 */
	private ProductInventoryService productInventoryService;

	/**
	 * 是否强制刷新缓存
	 */
	private Boolean forceRefresh;

	public ProductInventoryCacheRefreshRequest(Integer productId, ProductInventoryService productInventoryService,
			Boolean forceRefresh) {
		this.productId = productId;
		this.productInventoryService = productInventoryService;
		this.forceRefresh = forceRefresh;
	}

	@Override
	public void process() {
		log.info("刷新缓存 >>>>>> 读取数据库库存商品id：" + productId);
		// 从数据库中查询最新的库存信息
		ProductInventory productInventory = productInventoryService.findProductInventory(productId);
		log.info("刷新缓存 >>>>>> 将库存更新到redis缓存中，商品id：" + productInventory.getProductId() + "，库存："
				+ productInventory.getInventoryCnt());
		// 将查询出来的商品库存刷新到redis缓存中
		productInventoryService.setProductInventoryCache(productInventory);
	}

	@Override
	public Integer getProductId() {
		return productId;
	}

	@Override
	public Boolean getForceRefresh() {
		return forceRefresh;
	}

}
